package com.kelvin.oocl.parser;

import java.text.ParseException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.JMSException;

import org.json.JSONObject;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.kelvin.oocl.Thread.Task;
import com.kelvin.oocl.bookproducer.BookProducer;
import com.kelvin.oocl.pojo.Book;
import com.kelvin.oocl.util.DateUtil;
import com.kelvin.oocl.util.ProducerFactory;


public class BookHandler extends DefaultHandler{
	private String currTag = null;
	private Book book = null;
	private BookProducer producer;
	ExecutorService threadPool;

	@Override
	public void startDocument() throws SAXException {
		System.out.println("doc start");
		threadPool = Executors.newFixedThreadPool(15);
		producer = ProducerFactory.newInstance().newBookProducer("books");
	}

	@Override
	public void endDocument() throws SAXException {
		new Thread(){

			@Override
			public void run() {
				while(true){
					threadPool.shutdown();
					if(threadPool.isTerminated()){
						try {
							if(null!=producer){
								producer.close();
							}
							threadPool.shutdown();
						} catch (JMSException e) {
							e.printStackTrace();
						}
						break;
					}
				}
			}
			
		}.start();
		System.out.println("doc end");
		
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currTag = qName;
		if("book".equals(qName)){
			book = new Book();
			book.setId(Integer.parseInt(attributes.getValue(0)));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if("book".equals(qName)){
			String bookMsg = toJsonStr(book);
			System.out.println(bookMsg);
			Task task = new Task(producer, bookMsg);
			threadPool.submit(task);
		}
	}

	private String toJsonStr(Book book) {
		JSONObject object = new JSONObject();
		object.put("id", book.getId());
		object.put("bookName", book.getBookName());
		object.put("publishTime", DateUtil.dateToStr(book.getPublishTime(), "yyyy-MM-dd HH:mm:ss"));
		object.put("author", book.getAuthor());
		object.put("newBook", book.isNewBook());
		object.put("price", book.getPrice());
		object.put("press", book.getPress());
		return object.toString();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String text = new String(ch,start,length);
		try {
			if(!"".equals(text.trim())){
				if("publishTime".equals(currTag)){
					book.setPublishTime(DateUtil.strToDate(text, "yyyy-MM-dd HH:mm:ss"));
				}else if("bookName".equals(currTag)){
					book.setBookName(text);
				}else if("author".equals(currTag)){
					book.setAuthor(text);
				}else if("newBook".equals(currTag)){
					book.setNewBook(Boolean.parseBoolean(text));
				}else if("price".equals(currTag)){
					book.setPrice(Double.parseDouble(text));
				}else if("press".equals(currTag)){
					book.setPress(text);
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
}
